package io.renren.membermanage.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.annotation.RepeatSubmit;
import io.renren.membermanage.bo.ModelTrainParamBo;
import io.renren.membermanage.bo.PageResultBO;
import io.renren.membermanage.dto.*;
import io.renren.membermanage.entity.Model;
import io.renren.membermanage.entity.ModelLabel;
import io.renren.membermanage.entity.ModelTrain;
import io.renren.membermanage.entity.ModelTrainDetail;
import io.renren.membermanage.service.ModelService;
import io.renren.util.ApiResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/model")
@Tag(name = "模型管理")
public class ModelController {

    @Resource
    private ModelService modelService;

    @SaCheckLogin
    @PostMapping("/add")
    @Operation(summary = "新增模型", description = "新增模型")
    public ApiResult<String> addModel(@RequestParam("picture") MultipartFile picture,
                                      @RequestParam("modelFile") MultipartFile modelFile, @ModelAttribute AddModelParam param) {
        modelService.addModel(picture, modelFile, param);
        return ApiResult.success("success");
    }

    @SaCheckLogin
    @PostMapping("/update")
    public ApiResult<String> updateModel(@RequestParam(value="picture",required=false) MultipartFile picture,
                                      @RequestParam(value="modelFile",required = false) MultipartFile modelFile , @ModelAttribute UpdateModelParam param) {
        modelService.updateModel(picture, modelFile, param);
        return ApiResult.success("success");
    }

    @SaCheckLogin
    @DeleteMapping("/delete/{id}")
    public ApiResult<String> updateModel(@PathVariable Long id) {
        modelService.deleteModel(id);
        return ApiResult.success("success");
    }

    @SaCheckLogin
    @PostMapping("/addbytrain")
    @Operation(summary = "新增模型", description = "新增模型")
    public ApiResult<String> addModelByTrain(@RequestParam("picture") MultipartFile picture,
                                             @RequestParam("modelFileName") String modelName,
                                             @ModelAttribute AddModelParam param) {
        modelService.addModelByTrain(picture, modelName, param);
        return ApiResult.success("success");
    }

    @GetMapping("/getModelLabel")
    @Operation(summary = "查询全部模型标签", description = "查询全部模型标签")
    public ApiResult<List<ModelLabel>> getModelLabel() {
        return ApiResult.success(modelService.getModelLabel());
    }

    @SaCheckLogin
    @GetMapping("/getModels")
    @Operation(summary = "获取模型列表", description = "获取模型列表")
    @ApiResponses(value ={
            @ApiResponse(responseCode = "200", description = "请求成功",
                    content = @Content(mediaType = "application/json", schema = @Schema( implementation = PageResultBO.class))),
            @ApiResponse(responseCode = "400", description = "请求失败", content = @Content)
    })
    public ApiResult<PageResultBO> getModels(QueryModelParam param) {
        Page<Model> pageInfo = modelService.getList(param);
        return ApiResult.success(new PageResultBO(pageInfo.getRecords(), pageInfo.getTotal(), pageInfo.getCurrent(), pageInfo.getSize()));
    }

    @RepeatSubmit(interval = 300)
    @SaCheckLogin
    @PostMapping("/excuteTrain")
    public ApiResult<String> excuteTrain(@RequestBody ModelTrainParamBo bo) {
//        String key = RedisKeyConstant.getRepeatSubmit() + bo.getUserId();
//        if (redisUtil.setIfAbsent(key, "", 300)) {
//            boolean flag = modelService.excuteTrain(bo);
//            if (flag){
//                return ApiResult.success("模型训练任务创建成功！");
//            }else {
//                return ApiResult.success("模型训练任务创建失败，请联系相关人员！");
//            }
//        }else {
//            throw new BizSystemException(500, "您已经创建了训练任务，请等待任务完成再进行操作！");
//        }
        boolean flag = modelService.excuteTrain(bo);
        if (flag){
            return ApiResult.success("模型训练任务创建成功！");
        }else {
            return ApiResult.success("模型训练任务创建失败，请联系相关人员！");
        }
    }

    @SaCheckLogin
    @GetMapping("/getSchedule")
    public ApiResult<String> getSchedule(@RequestParam("taskId") String taskId){
        return ApiResult.success(modelService.getSchedule(taskId));
    }

    @SaCheckLogin
    @GetMapping("/getModelTrain")
    public ApiResult<Page<ModelTrain>> getModelTrain(@Validated QueryModelTrainDto dto){
        return ApiResult.success(modelService.getModelTrain(dto));
    }
    @SaCheckLogin
    @GetMapping("/getModelTrainDetail")
    public ApiResult<List<ModelTrainDetail>> getModelTrain(@Validated QueryModelTrainDetailDto dto){
        return ApiResult.success(modelService.getModelTrainDetail(dto));
    }
}
